/*
 *@本JS功能：班级管理
 *@本js作者：杨锡君
 *@编写时间：2017年10月31日
 */
$(function() {
	var Alert1 = new Alert();
	var confirm1 = new Confirm();
    var confirm2 = new Confirm();
	var loading1 = new Loading();
	loading1.init();
	var $departNames = $("#departNames");
	var $tableBox = $(".tableBox");
	var $searchBtn = $(".searchBtn");
	var $exportEvent = $(".exportBtn");
	var $exportCancle = $(".exportCancle");
	var $fileName = $(".fileName");
	var jsonName;
	var th = "<tr>" +
		"<th class=\"col-lg-1\"><input type=\"checkbox\" name=\"controlAll\" id=\"controlAll\" class=\"allCheck\"></th>" +
        "<th class=\"col-lg-2\">专业</th>" +
        "<th class=\"col-lg-2\">专业方向</th>" +
		"<th class=\"col-lg-2\">课程名称</th>" +
        "<th class=\"col-lg-2\">修改操作</th>" +
		"<th class=\"col-lg-3\">发布操作</th>" +
		"</tr>";
	function tabelModel(opt) {
		var str =  "<tr data-num=\"0\">" +
			"<td class=\"col-lg-1\"><input data-courseid=" + opt.courseId + " value=\"0\" name=\"selecteds\" type=\"checkbox\" class=\"oneCheck\" /></td>" +
            "<td class=\"col-lg-2\" title=" + opt.professionName + ">" + opt.professionName + "</td>" +
            "<td class=\"col-lg-2\" title=" + opt.directName + ">" + opt.directName + "</td>" +
			"<td class=\"col-lg-3\" title=" + opt.courseName + ">" + opt.courseName + "</td>" +
			"<td class=\"col-lg-2\">" +
			"<a href=\"javascript:;\" data-courseid=" + opt.courseId + "  class=\"editBtn\">课程修改</a>&ensp;" +
            "<a href=\"javascript:;\" data-courseid=" + opt.courseId + "  class=\"detailBtn\">章节修改</a>" +
			"</td>" +
            "<td class=\"col-lg-2\">";
			if(opt.releaseMark==0){
				str += "<a href=\"javascript:;\" data-courseid=" + opt.courseId + "  class=\"publish\">发布</a>&ensp;";
			}else{
				str += "<a href=\"javascript:;\" data-courseid=" + opt.courseId + "  class=\"cancel\">取消</a>&ensp;";
			}
           if(opt.releaseMark==0){
				str+=  "<a style='color: #b5b5b5;' href=\"javascript:;\" data-courseid=" + opt.courseId + "  class=\"share\"><span  data-courseid=" + opt.courseId + " class=\"switch-off switch-disabled \"></span>&ensp;共享</a>";
		   }else{
				if(opt.shareMark == 0){
                    str+=  "<a href=\"javascript:;\" data-courseid=" + opt.courseId + "  class=\"share\"><span  data-courseid=" + opt.courseId + " class=\"switch-off \"></span>&ensp;共享</a>";
                }else{
                    str+=  "<a href=\"javascript:;\" data-courseid=" + opt.courseId + "  class=\"share\"><span  data-courseid=" + opt.courseId + " class=\"switch-on \"></span>&ensp;共享</a>";

                }
		   }
            str+="</td>" +
			"</tr>";

		return str;
	}
	var classMana = {
	    some:'',
		init: function() {
			this.bindEvent();
            maxLengthLimit.init("publicText");
		},
		bindEvent: function() {
			var self = this;
			var json = {
				"pageNow": 1,
				"pageSize": 10
			};
            if(result.paramData!=undefined){
                if(result.paramData.some!=undefined){
                    json = result.paramData.some;
                    if(json.courseName!=undefined){
                        $("#courseName").val(json.courseName);
                    }
                }
            }
            self.some = json;
            jsonName = json;
			self.tabelList(json);
			$(".delClass").off("click").on("click", function() {
				self.delListEvent();
			});
			$searchBtn.off("click").on("click", function() {
				var courseName = $("#courseName").val();
				var flag = verification.spaceCode(courseName);
				var flags = verification.specialCode(courseName);
				if(flag != true) {
					Alert1.content = flag;
					$("#courseName").val("");
					Alert1.init();
					Alert1.show();
					return false;
				}
				if(flags != true) {
					Alert1.content = flags;
					$("#courseName").val("");
					Alert1.init();
					Alert1.show();
					return false;
				}
				var jsonStr = {
					"pageNow": 1,
					"pageSize": 10
				};
				if(courseName != "") {
					jsonStr.courseName = courseName;
				}
                jsonName = jsonStr;
                self.some = jsonStr;
				self.tabelList(jsonStr);
			});
            $(".addCourse").off("click").on("click", function() {
                var json = JSON.stringify({
                    "pageName":"courseMake",
                    "paramData":{
                        "some":self.some
                    }
                });
                window.location.href = "skipPage?jsonStr="+json;
            });
		},
		switchEvent:function () {
            $("[class^=switch]").off("click").click(function() {
                if ($(this).hasClass("switch-disabled")) {
                    return;
                }
                var that = $(this);
                if ($(this).hasClass("switch-on")) {
                	var json = JSON.stringify({"shareMark":0,"list":[Number(that.attr("data-courseid"))]});
                	$.ajax({
						type:'post',
						url:'shareCourse',
                        async: true,
						data:{
							"jsonStr":json
						},
						success:function (msg) {
							msg = JSON.parse(msg);
							if(msg.result == "success"){
                                that.removeClass("switch-on").addClass("switch-off");
                                $(".switch-off").css({
                                    'border-color' : '#dfdfdf',
                                    'box-shadow' : 'rgb(223, 223, 223) 0px 0px 0px 0px inset',
                                    'background-color' : 'rgb(255, 255, 255)'
                                });
							}else{
                                if(msg.failMsg != undefined){
                                    if(msg.failMsg == "暂无权限"){
                                        parent.location.reload(true);
                                    }else{
                                        Alert1.content = msg.failMsg;
                                        Alert1.init();
                                        Alert1.show();
                                    }
                                }else{
                                    if (msg.errorMsg != "" && msg.errorMsg != undefined) {
                                        Alert1.content = msg.errorMsg;
                                    } else {
                                        Alert1.content = "获取失败";
                                    }
                                    Alert1.init();
                                    Alert1.show();
                                }
							}
                        }
					});


                } else {
                    var json = JSON.stringify({"shareMark":1,"list":[Number(that.attr("data-courseid"))]});
                    $.ajax({
                        type:'post',
                        url:'shareCourse',
                        async: true,
                        data:{
                            "jsonStr":json
                        },
                        success:function (msg) {
                            msg = JSON.parse(msg);
                            if(msg.result == "success"){
                                that.removeClass("switch-off").addClass("switch-on");
                                if (honeySwitch.themeColor) {
                                    var c = honeySwitch.themeColor;
                                    that.css({
                                        'border-color' : c,
                                        'box-shadow' : c + ' 0px 0px 0px 16px inset',
                                        'background-color' : c
                                    });
                                }
                                if ($(this).attr('themeColor')) {
                                    var c2 = that.attr('themeColor');
                                    that.css({
                                        'border-color' : c2,
                                        'box-shadow' : c2 + ' 0px 0px 0px 16px inset',
                                        'background-color' : c2
                                    });
                                }
                            }else{
                                if(msg.failMsg != undefined){
                                    if(msg.failMsg == "暂无权限"){
                                        parent.location.reload(true);
                                    }else{
                                        Alert1.content = msg.failMsg;
                                        Alert1.init();
                                        Alert1.show();
                                    }
                                }else{
                                    if (msg.errorMsg != "" && msg.errorMsg != undefined) {
                                        Alert1.content = msg.errorMsg;
                                    } else {
                                        Alert1.content = "获取失败";
                                    }
                                    Alert1.init();
                                    Alert1.show();
                                }
                            }
                        }
                    });

                }
            });
        },
		tabelList: function(jsons) {
			var self = this;
			var json = JSON.stringify(jsons);
			loading1.show();
			$.ajax({
				type: "post",
				url: "skipTeaCourseMana",
				data: {
					"jsonStr": json
				},
				async: true,
				success: function(msg) {
					loading1.hide();
					msg = JSON.parse(msg);
					$tableBox.empty();
					if(msg.result == "success"){
                        if(msg.courseList.length != 0) {
                            //列表
                            var tabel = "";
                            $.map(msg.courseList, function(opt) {
                                tabel += tabelModel(opt);
                            });
                            $tableBox.append(th + tabel);
                            self.allCheckEvent();
                            self.skipEvent();
                            honeySwitch.init();
                            self.switchEvent();
                        } else {
                            var trImg = "<tr><td colspan='6'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td></tr>";
                            $tableBox.append(th + trImg)
                        }
                        var sumpage = msg.totalPage;
                        if(sumpage == 0){
                            sumpage = 1;
                        }
                        var nowpage = msg.pageNow;
                        var pagenumber = 10;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {
                                nowpage = p;
                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                var jsonStr = jsons;
                                jsonStr.pageNow = nowpage;
                                jsonName = jsonStr;
                                loading1.show();
                                $.ajax({
                                    type: "post",
                                    url: "skipTeaCourseMana",
                                    data: {
                                        "jsonStr":JSON.stringify(jsonStr)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        loading1.hide();
                                        msg = JSON.parse(msg);
                                        //列表
                                        $tableBox.empty();
                                        var tabel = "";
                                        $.map(msg.courseList, function(opt) {
                                            tabel += tabelModel(opt);
                                        });
                                        $tableBox.append(th + tabel);
                                        self.allCheckEvent();
                                        self.skipEvent();
                                        honeySwitch.init();
                                        self.switchEvent();
                                    }
                                });
                            }
                        });
					}else{
                        if(msg.failMsg != undefined){
                            if(msg.failMsg == "暂无权限"){
                                parent.location.reload(true);
                            }else{
                                Alert1.content = msg.failMsg;
                                Alert1.init();
                                Alert1.show();
                            }
                        }else{
                            if (msg.errorMsg != "" && msg.errorMsg != undefined) {
                                Alert1.content = msg.errorMsg;
                            } else {
                                Alert1.content = "获取失败";
                            }
                            Alert1.init();
                            Alert1.show();
                        }
					}


				}
			});

		},
		skipEvent:function(){
			var self = this;
//			修改课程
			$(".editBtn").off("click").click(function(){
				var json = JSON.stringify({
					"pageName":"courseSave",
					"paramData":{
						"courseId":$(this).attr("data-courseid"),
                        "some":self.some
					}
				});
				window.location.href = "skipPage?jsonStr="+json;
			});
            $(".detailBtn").off("click").click(function(){
                var json = JSON.stringify({
                    "pageName":"courseChapterEdit",
                    "paramData":{
                        "courseId":$(this).attr("data-courseid"),
						"pageName":"preserveCourse",
                        "some":self.some
                    }
                });
                window.location.href = "skipPage?jsonStr="+json;
            });
            $(".publish").off("click").click(function () {
            	var json = JSON.stringify({"releaseMark":1,"list":[Number($(this).attr("data-courseid"))]});
                loading1.show();
                $.ajax({
                    type: "post",
                    url: "releaseCourse",
                    async: true,
                    data: {
                        "jsonStr":json
                    },
                    success: function(msg) {
                        loading1.hide();
                        msg = JSON.parse(msg);
                        if(msg.result == "success"){
                            Alert1.content = "发布成功";
                            Alert1.success = function () {
                                self.tabelList(jsonName);
                            };
                            Alert1.init();
                            Alert1.show();
                        }else{
                            Alert1.content = msg.failMsg;
                            Alert1.init();
                            Alert1.show();
                        }

                    },
                    error: function(err) {

                    }

                });
            });
            $(".cancel").off("click").click(function () {
                var json = JSON.stringify({"releaseMark":0,"list":[Number($(this).attr("data-courseid"))]});
                loading1.show();
                $.ajax({
                    type: "post",
                    url: "releaseCourse",
                    async: true,
                    data: {
                        "jsonStr":json
                    },
                    success: function(msg) {
                        loading1.hide();
                        msg = JSON.parse(msg);
                        if(msg.result == "success"){
                            Alert1.content = "取消发布成功";
                            Alert1.success = function () {
                                self.tabelList(jsonName);
                            };
                            Alert1.init();
                            Alert1.show();
                        }else{
                            Alert1.content = msg.failMsg;
                            Alert1.init();
                            Alert1.show();
                        }

                    },
                    error: function(err) {

                    }

                });
            });
		},
		delListEvent: function() {
            var self = this;
			if($(".oneCheck[value=1]").length == 0) {
				Alert1.content = "请选择要删除的课程";
				Alert1.init();
				Alert1.show();
			} else {
				var list = [];
					for(var i = 0; i < $(".oneCheck[value=1]").length; i++) {
						list.push(Number($(".oneCheck[value=1]").eq(i).attr("data-courseid")));
				}
                $.ajax({
                    type: "post",
                    url: "getDelCourseMark",
                    async: true,
                    data: {
                        "jsonStr": JSON.stringify(list)
                    },
                    success: function (msg) {
                        msg = JSON.parse(msg);
                        if (msg.result == "success") {
                                $.ajax({
                                    type: "post",
                                    url: "delCourseHivePower",
                                    async: true,
                                    data: {
                                        "jsonStr": JSON.stringify(list)
                                    },
                                    success: function (msg) {
                                        msg = JSON.parse(msg);
                                        if(msg.result == "success"){
                                            confirm1.content = "确认删除当前选中课程吗？";
                                            confirm1.success = function () {
                                                var jsonStr = JSON.stringify(list);
                                                loading1.show();
                                                $.ajax({
                                                    type: "post",
                                                    url: "delCourse",
                                                    async: true,
                                                    data: {
                                                        "jsonStr":jsonStr
                                                    },
                                                    success: function(msg) {
                                                        loading1.hide();
                                                        msg = JSON.parse(msg);
                                                        if(msg.result == "success"){
                                                            Alert1.content = "删除成功";
                                                            Alert1.success = function () {
                                                                self.tabelList(self.some);
                                                            };
                                                            Alert1.init();
                                                            Alert1.show();
                                                        }else{
                                                            Alert1.content = "删除失败";
                                                            Alert1.init();
                                                            Alert1.show();
                                                        }

                                                    },
                                                    error: function(err) {

                                                    }

                                                });
                                            };
                                            confirm1.init();
                                            confirm1.show();

                                        }else{
                                            confirm1.content = msg.failMsg;
                                            confirm1.success = function() {
                                                var jsonStr = JSON.stringify(list);
                                                loading1.show();
                                                $.ajax({
                                                    type: "post",
                                                    url: "delCourse",
                                                    async: true,
                                                    data: {
                                                        "jsonStr":jsonStr
                                                    },
                                                    success: function(msg) {
                                                        loading1.hide();
                                                        msg = JSON.parse(msg);
                                                        if(msg.result == "success"){
                                                            Alert1.content = "删除成功";
                                                            Alert1.success = function () {
                                                                self.tabelList(self.some);
                                                            };
                                                            Alert1.init();
                                                            Alert1.show();
                                                        }else{
                                                            Alert1.content = "删除失败";
                                                            Alert1.init();
                                                            Alert1.show();
                                                        }

                                                    },
                                                    error: function(err) {

                                                    }

                                                });
                                            };
                                            confirm1.init();
                                            confirm1.show();
                                        }
                                    }
                                });
                        }else{
                            confirm2.content = msg.failMsg;
                            confirm2.success = function() {
                                $.ajax({
                                    type: "post",
                                    url: "delCourseHivePower",
                                    async: true,
                                    data: {
                                        "jsonStr": JSON.stringify(list)
                                    },
                                    success: function (msg) {
                                        msg = JSON.parse(msg);
                                        if(msg.result == "success"){
                                                var jsonStr = JSON.stringify(list);
                                                loading1.show();
                                                $.ajax({
                                                    type: "post",
                                                    url: "delCourse",
                                                    async: true,
                                                    data: {
                                                        "jsonStr":jsonStr
                                                    },
                                                    success: function(msg) {
                                                        loading1.hide();
                                                        msg = JSON.parse(msg);
                                                        if(msg.result == "success"){
                                                            Alert1.content = "删除成功";
                                                            Alert1.success = function () {
                                                                self.tabelList(self.some);
                                                            };
                                                            Alert1.init();
                                                            Alert1.show();
                                                        }else{
                                                            Alert1.content = "删除失败";
                                                            Alert1.init();
                                                            Alert1.show();
                                                        }

                                                    },
                                                    error: function(err) {

                                                    }

                                                });

                                        }else{
                                            confirm1.content = msg.failMsg;
                                            confirm1.success = function() {
                                                var jsonStr = JSON.stringify(list);
                                                loading1.show();
                                                $.ajax({
                                                    type: "post",
                                                    url: "delCourse",
                                                    async: true,
                                                    data: {
                                                        "jsonStr":jsonStr
                                                    },
                                                    success: function(msg) {
                                                        loading1.hide();
                                                        msg = JSON.parse(msg);
                                                        if(msg.result == "success"){
                                                            Alert1.content = "删除成功";
                                                            Alert1.success = function () {
                                                                self.tabelList(self.some);
                                                            };
                                                            Alert1.init();
                                                            Alert1.show();
                                                        }else{
                                                            Alert1.content = "删除失败";
                                                            Alert1.init();
                                                            Alert1.show();
                                                        }

                                                    },
                                                    error: function(err) {

                                                    }

                                                });
                                            };
                                            confirm1.init();
                                            confirm1.show();
                                        }
                                    }
                                });
                            };
                            confirm2.init();
                            confirm2.show();
                        }
                    }
                });



			}
		},
		allCheckEvent: function() {
			//全选
			allCheck.init("controlAll", "oneCheck");
		}
	};
	classMana.init();
});